package com.lovi.thread_;

import jdk.nashorn.internal.ir.Flags;

import java.util.Scanner;

/*
1)在main方法中启动两个线程
2)第1个线程循环随机打印100以内的整数
3）直到第2个线程从键盘读取了“Q”命令。
 */
public class Thread07 {
    public static void main(String[] args) {
        EE ee = new EE();
        Thread thread = new Thread(ee);
        thread.start();
        FF ff = new FF();
        ff.setEE(ee);
        new Thread(ff).start();


    }

}
class EE implements Runnable{

    private boolean flag = true;

    @Override
    public void run() {
        while (flag){
            System.out.println((int)(Math.random()*100) + 1);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}

class FF implements Runnable{
    private EE ee;
    private Scanner sc = new Scanner(System.in);
    public void setEE(EE ee){
        this.ee = ee;
    }
    @Override
    public void run() {
        while (true) {
            System.out.println("请输入Q退出");
            String next = sc.next();
            if (next.equals("Q")) {
                if (ee != null) {
                    ee.setFlag(false);
                    break;
                }
            }
        }
    }
}
